﻿
var firefive =
{
	onLoad: function()
	{
		// 初始化
		this.initialized = true;
		this.strings = document.getElementById("firefive-strings");
	},

	load: function()
	{
		document.getElementById("urlbar").addEventListener("keyup", function (event) { firefive.url_key_press(event); }, false);
		document.getElementById("urlbar").addEventListener("focus", function () { firefive.url_get_focus(); }, false);
	},

	url_get_focus : function ()
	{
		// 地址栏获得焦点
	},

	with_url_update : function ()
	{
		alert("左侧按钮被点击");
	},

	update : function ()
	{
		alert("右侧按钮被点击");
/*
由于 api_key 没有申请下来，且 api 调用部分没有弄好，所以就先用 5享 顶替一下。
*/
		document.getElementById("firefive-update").src = "chrome://firefive/skin/waiting.gif";

		var url = document.getElementById("urlbar").value;

		window.open(
		 'http://www.follow5.com/f5/jsp/plugin/5share/5ShareLogin.jsp?title=' + encodeURIComponent(content.document.title)+ '&url=' + encodeURIComponent(url) , '_blank','width=560,height=270,top=200,%20left=300,%20toolbar=no,%20menubar=no,%20scrollbars=no,%20resizable=no,location=no,%20status=no'
		);

	},

	icon_moveon : function ()
	{
		document.getElementById("firefive-withurl-update-button").hidden = false;
		document.getElementById('firefive-update').src = "chrome://firefive/skin/follow5_mouseon.png"
		var state_label = document.getElementById('firefive-count')
		
		state_label.hidden = false;

		var length = this.get_url_count();

		if (length > 140)
		{
			state_label.style.color = "red";
			state_label.value = "已超过 " + ( length - 140 ) + " 字。";
		}
		else
		{
			state_label.style.color = "green";
			state_label.value = "还可以输入 " + ( 140 - length ) + " 字。";
		}

	},

	icon_moveout : function ()
	{
		var image = document.getElementById('firefive-update');

		if (image.src.match(/follow5_mouseon\.png/))
		{
			image.src =	"chrome://firefive/skin/follow5.png"
		}

		document.getElementById('firefive-count').hidden = true;
		document.getElementById("firefive-withurl-update-button").hidden = true;

	},

	get_url_count : function ()
	{
		// 计算地址栏字串的长度
		var status = document.getElementById("urlbar").value;
		length = status.length;
		return length;
	},

	url_key_press : function (e)
	{
		// 在地址栏按键
		this.icon_moveon();
	},

	onMenuItemCommand: function(e)
	{
		var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
																	.getService(Components.interfaces.nsIPromptService);
		promptService.alert(window, this.strings.getString("helloMessageTitle"),
																this.strings.getString("helloMessage"));
	},

	onToolbarButtonCommand: function(e)
	{
		// 重用上面的函数，当然也可以修改这里
		firefive.onMenuItemCommand(e);
	}
};

window.addEventListener("load", firefive.onLoad, false);
